#include <iostream>
#include <cstdio>
#include <cstring>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

void Usage()
{
    printf("Using Warning , Please use the correct manual: \n ./server.cc ip port\n");
}

// ./client.cc 0.0.0.0 8080
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage();
        exit(1);
    }

    // 要向哪一个ip和端口号发送数据
    std::string ip = argv[1];
    uint16_t port = atoi(argv[2]);

    // 创建套接字
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0)
    {
        // 创建套接字失败
        std::cerr << "create socket error, errno: " << errno << " strerrno : " << strerror(errno) << std::endl;
        exit(1);
    }

    // sockaddr_in
    sockaddr_in addr;
    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = inet_addr(ip.c_str());

    //发送消息
    std::string str;
    while (true)
    {
        std::cout << "请输入消息: ";
        getline(std::cin, str);
        sendto(sockfd, str.c_str(), str.size(), 0, (struct sockaddr*)&(addr), sizeof(addr));       
    }

    return 0;
}